package bupt.party.online.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import bupt.party.online.model.PartyNews;
import bupt.party.online.model.Student;
import bupt.party.online.service.IBranchService;
import bupt.party.online.service.ILearnCenterService;
import bupt.party.online.service.IPartyNewsService;
import bupt.party.online.service.IStudentService;
import bupt.party.online.util.SessionConstants;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ognl.OgnlValueStack;

import bupt.party.online.model.LearnCenter;
import bupt.party.online.video.Video;
import bupt.party.online.video.VideoMgr;

public class IndexAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
    private HttpServletResponse response; 
    
    private IBranchService  branchService;
	private IStudentService studentService;
	private IPartyNewsService partyNewsService;
	private ILearnCenterService learnCenterService;
	
	private List<PartyNews> pictureNews;
	private List<LearnCenter> notices;   //首页的学习公告列表
	
	public List<PartyNews> getPictureNews() {
		return pictureNews;
	}

	public void setPictureNews(List<PartyNews> pictureNews) {
		this.pictureNews = pictureNews;
	}

	public IPartyNewsService getPartyNewsService() {
		return partyNewsService;
	}

	public void setPartyNewsService(IPartyNewsService partyNewsService) {
		this.partyNewsService = partyNewsService;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
		request.getRequestURI();
		request.getQueryString();
	}
	
	public IStudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	public List<LearnCenter> getNotices() {
		return notices;
	}

	public void setNotices(List<LearnCenter> notices) {
		this.notices = notices;
	}

	public ILearnCenterService getLearnCenterService() {
		return learnCenterService;
	}

	public void setLearnCenterService(ILearnCenterService learnCenterService) {
		this.learnCenterService = learnCenterService;
	}

	public IBranchService getBranchService() {
		return branchService;
	}

	public void setBranchService(IBranchService branchService) {
		this.branchService = branchService;
	}

	/**
	 *  进入首页
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String index(){
		 
		HttpSession session = request.getSession();
	    Integer level = (Integer) session.getAttribute(SessionConstants.LEVEL);
	    
	    // 为首页准备数据
	    List<Video> hostestVideos = VideoMgr.getInstance().getHotestVideos();
	    Video lastestVideo = VideoMgr.getInstance().getLastestVideo();
	    request.setAttribute("hostestVideos", hostestVideos);
	    request.setAttribute("lastestVideo", lastestVideo);
	    
	    // Prepare latest videos
	    List<Video> latestVideos = VideoMgr.getInstance().getLastest3Video();
	    request.setAttribute("latestVideos", latestVideos);
	    
	    if(level != null && level.equals(SessionConstants.STU_LEVEL)){
	    	
	    	  request.setAttribute("sessionCount", this.sessionCount());
		 	  
	    	  pictureNews = this.partyNewsService.getLatest4PictureNews();
	    	  this.notices = this.learnCenterService.getLatest4Notices();
	    		
	    	  Student  session_student = (Student) session.getAttribute(SessionConstants.SESSION_USER);
	    	  int branchId = session_student.getStudentBranchId();
	    	  String branchName = branchService.getBranchName(branchId);
	    	  request.setAttribute("branchName", branchName);
	    			  
		      return "home";
		      
	    }else {
	    	
	    	session.setAttribute(SessionConstants.SESSION_USER, null);
	    	
			session.setAttribute(SessionConstants.LEVEL, null);
			
			pictureNews = this.partyNewsService.getLatest4PictureNews();
			this.notices = this.learnCenterService.getLatest4Notices();

	    	return  "index";
	    }
	}
	
	public int sessionCount() {
		Map appMap = ServletActionContext.getContext().getApplication();
		Map sessionMap = (Map) appMap.get("sessionMap");
		if (sessionMap == null) {
			return 0;
		}
		
		return sessionMap.keySet().size();
	}

	
	
	/**
	 * Denied if returnStr is 0, while proved if returnStr is 1
	 */
	public void queryAccess(){
		HttpServletRequest request = ServletActionContext.getRequest(); 
//		HttpSession session = request.getSession();
		String userId = request.getParameter("userId");
		String menuId = request.getParameter("menuId");
		
		// Temp to added here, and will be removed!!
//		System.out.println("PermissionAction::queryAccess: userId:" + userId + "; menuId:" + menuId);
		// End of temp code
		
		int resultStr = queryAccess(userId, menuId);
		
		// Write back
		OgnlValueStack stack = (OgnlValueStack) request.getAttribute("struts.valueStack");
		HttpServletResponse response = (HttpServletResponse) stack.getContext().get(StrutsStatics.HTTP_RESPONSE);
		try {
			response.setHeader("Content-Type", "text/plain; charset=UTF-8");
			response.getWriter().write(String.valueOf(resultStr));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Will be added by database
	 * @param userId
	 * @param menuId
	 * @return
	 */
	private int queryAccess(String userId, String menuId){
		return 1;
	}
	
	public String getShortTitle(String title){
		
		if(title.length() > 12) title = title.substring(0,11) + "...";
		
		return  title;
		
	}
	
	public String getShortContent(String content,int length){
		
		if(content == null) content = "";
		
		content = content.replaceAll("<[^>]*>", "");
		
		if(content.length() > length) content = content.substring(0, length - 1) + "...";
		
		return content;
}
	
}
